# -*- coding: utf-8 -*-
"""
@Time ： 2020/8/14 10:44
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：logging日志打印模块
"""
import logging
import yaml


def init(path):
    global logger
    # file = open(file="../lib/conf.yaml", mode='r', encoding="utf-8")
    with open(file=path + "lib/conf.yaml", mode='r', encoding="utf-8")as file:
        logging_yaml = yaml.safe_load(stream=file).get('logger')
        logging_yaml['filename'] = path + logging_yaml.get('filename')
        # print(logging_yaml)
        # 配置logging日志：主要从文件中读取handler的配置、formatter（格式化日志样式）、logger记录器的配置
        logging.basicConfig(**logging_yaml)

    # 获取根记录器：配置信息从yaml文件中获取
    logger = logging.getLogger()

    # 创建输出到控制台的输出流
    console = logging.StreamHandler()
    # 设置日志等级
    console.setLevel(logging_yaml['level'])
    # 设置日志格式
    console.setFormatter(logging.Formatter(logging_yaml['format']))
    # 添加到logger输出
    logger.addHandler(console)


def debug(log):
    logger.debug(log)


def info(log):
    logger.info(log)


def warning(log):
    logger.warning(log)


def error(log):
    logger.error(log)


def exception(log):
    logger.exception(log)


if __name__ == "__main__":
    init('../')
    # 等级顺序
    logger.debug("DEBUG")
    logger.info("INFO")
    logger.warning('WARNING')
    logger.error('ERROR')
    try:
        int('a')
    except Exception as e:
        logger.exception(e)
